// index.js
import { translate } from '../../utils/baidu-fanyi-api.js'

//获取应用实例
const app = getApp()

Page({
  //页面初始数据
  data: {
    curLang: {},
    hideCloseIcon: true,
    query: '',
    translateReault: '',
    prefix: {},
    history: []
  },

  //监听界面加载
  onLoad: function (options) {
    console.log('options quary：', options.query)
    if (options.query) {
      this.setData({
        query: options.query
      })
    }
  },

  //监听界面显示
  onShow: function () {
    if (this.data.curLang.lang !== app.globalData.curLang.lang) {
      this.setData({
        curLang: app.globalData.curLang
      })
      this.onConfirm();
    }
  },

  //输入数据
  onInput: function (e) {
    this.setData({
      query: e.detail.value
    })
    if (this.data.query.length > 0) {
      this.setData({
        hideCloseIcon: false
      })
    } else {
      this.setData({
        hideCloseIcon: true
      })
    }
  },

  //确认输入完成，调用百度翻译API
  onConfirm: function () {
    if (!this.data.query) { return }
    translate(
      this.data.query,
      {
        from: 'auto',
        to: this.data.curLang.lang
      }
    ).then(data => {
      console.log(data)
      console.log(app.globalData.used)
      if (!app.globalData.used) {
        wx.setStorageSync('used', true)
        wx.cloud.callFunction({
          name: 'history',
          data: {
            type: "insertRecord",
            openid: app.globalData.openid
          }
        }).then((res) => {
          console.log('添加新用户')
        })
      }
      wx.cloud.callFunction({
        name: 'history',
        data: {
          type: 'selectRecord',
          openid: app.globalData.openid
        }
      }).then((res) => {
        console.log("history:", res.result.data[0].history)
        this.setData({
          history: res.result.data[0].history,
        })
        console.log("length:", this.data.history.length)
        if (this.data.history.length) {
          this.setData({
            prefix: res.result.data[0].history[0]
          })
        }

        let resultArray = []
        for (let i = 0; i < data.trans_result.length; i++) {
          resultArray.push(data.trans_result[i])
        }
        console.log('resultArray:', resultArray)
        let src = []
        let dst = []
        resultArray.forEach(key => {
          src.push(key['src'])
          dst.push(key['dst'])
        })
        let queryString = src.join('\n')
        let resultString = dst.join('\n')
        console.log(queryString, resultString)

        this.setData({
          translateResult: resultString
        })
        if (this.data.history.length === 0) {
          wx.cloud.callFunction({
            name: 'history',
            data: {
              type: 'updateRecord',
              openid: app.globalData.openid,
              record: {
                query: queryString,
                result: resultString
              }
            }
          })
        } else {
          let prefix = this.data.prefix
          if (prefix && prefix.query && prefix.result && queryString !== prefix.query || resultString !== prefix.result) {
            wx.cloud.callFunction({
              name: 'history',
              data: {
                type: 'updateRecord',
                openid: app.globalData.openid,
                record: {
                  query: queryString,
                  result: resultString
                }
              }
            })
          }
        }


      })
    }, error => {
      console.log(error)
    })
  },

  onTapClose: function () {
    this.setData({
      query: '',
      hideCloseIcon: true,
      translateReault: ''
    })
  }
})
